package uni.call.app;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.sip.SipException;
import android.net.sip.SipProfile;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class IncomingCallAnswerActivity extends Activity
{
	public static MediaPlayer RingTone; 
	
	public void onCreate(Bundle savedInstanceState)
	{	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.incomingcallanswer);
        
        SipProfile TempPeer =  IncomingCallReceiver.IncomingCall.getPeerProfile();
                
        TextView UserName = (TextView)findViewById(R.id.TempCallerID);
        TextView DisplayName = (TextView)findViewById(R.id.TempCallerName);

        DisplayName.setText(TempPeer.getUserName());
        UserName.setText(TempPeer.getDisplayName());
        
        
        ImageButton AnswerBT = (ImageButton)findViewById(R.id.Answer);
        ImageButton DeclineBT = (ImageButton)findViewById(R.id.Decline);
        
        AnswerBT.setOnClickListener(AnswerListener);
        DeclineBT.setOnClickListener(DeclineListener);
        
    	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    	switch (MyAudioManager.getRingerMode())
    	{
	    	case AudioManager.RINGER_MODE_NORMAL:
	    		RingTone = MediaPlayer.create(this, R.raw.ringtone);
	    		RingTone.start();
	            break;
    	}
	}
	private OnClickListener AnswerListener = new OnClickListener() 
	{
        public void onClick(View v)
        {
        	try
        	{
        		RingTone.stop();
        	}
        	catch (Exception e)
        	{
				// TODO: handle exception
			}
        	Intent MyIntent = new Intent(getApplicationContext(), IncomingCallActivity.class);
        	MyIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        	MyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        	startActivity(MyIntent);       
        }       
    };
    
    private OnClickListener DeclineListener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	try
        	{
        		RingTone.stop();
        	}
        	
        	catch (Exception e) 
        	{
				// TODO: handle exception
			}
        	SipProfile TempPeer =  IncomingCallReceiver.IncomingCall.getPeerProfile();
            
            final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String StartTimeDate = sdf.format(cal.getTime());
            
        	Insert("Canceled", TempPeer.getDisplayName(),TempPeer.getUserName(), "0 Sec", StartTimeDate);
        	try 
        	{   		
        		IncomingCallReceiver.IncomingCall.endCall();
        		IncomingCallReceiver.IncomingCall.close();
        		IncomingCallAnswerActivity.this.finish();
			} 
        	catch (SipException e)
			{
				e.printStackTrace();
			}
        }
    };
    public void Insert(String Type,String Name, String Number, String Duration, String Time)
    {	
    	LogDBAdapter myAdapter = new LogDBAdapter(getApplicationContext());
		myAdapter.OpenToWrite();        
		myAdapter.Insert(Type,Name,Number, Duration, Time);
		myAdapter.Close();
    }
}
